﻿using System;
using System.Text;
using System.Data;

using LTJ.Common.Generic;
using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;

namespace LTJ.Data.DataProvider
{
    /// <summary>
    /// 用户组操作接口
    /// </summary>
    public class UserGroup
    {
        /// <summary>
        /// 创建新用户组.
        /// </summary>
        /// <param name="usergroupinfo">用户组信息</param>
        /// <returns>返回用户组ID, 如果已存在该用户名则返回-1</returns>
        public static int CreateUserGroup(LTJ.Entity.UserGroupInfo usergroupinfo)
        {
            return DatabaseProvider.GetInstance().CreateUserGroup(usergroupinfo);
        }

        /// <summary>
        /// 根据用户组名返回用户组信息
        /// </summary>
        /// <param name="usergroupname">用户组名</param>
        /// <returns>用户id</returns>
        public static LTJ.Entity.UserGroupInfo GetUserGroupInfoByName(string usergroupname)
        {
            return LoadSingleUserGroupInfo(DatabaseProvider.GetInstance().GetUserGroupInfoByName(usergroupname));
        }
        /// <summary>
        /// 根据where条件 取得用户组数据列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public static DataTable GetUserGroupList(string strWhere)
        {
            return DatabaseProvider.GetInstance().GetUserGroupList(strWhere);
        }

        /// <summary>
        /// 删除用户组信息
        /// </summary>
        /// <param name="ids">用户组id组合</param>
        /// <returns></returns>
        public static int DelUserGroup(string ids)
        {
            return DatabaseProvider.GetInstance().DelUserGroup(ids);
        }

        /// <summary>
        /// 获取用户组实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static LTJ.Entity.UserGroupInfo GetUserGroupInfo(int id)
        {
            return LoadSingleUserGroupInfo(DatabaseProvider.GetInstance().GetUserGroupInfoByID(id));
        }
        /// <summary>
        /// 编辑用户组
        /// </summary>
        public static bool UpdateUserGroup(LTJ.Entity.UserGroupInfo ugInfo)
        {
            return DatabaseProvider.GetInstance().UpdateUserGroup(ugInfo);
        }
        #region helper
        public static LTJ.Entity.UserGroupInfo LoadSingleUserGroupInfo(IDataReader reader)
        {
            LTJ.Entity.UserGroupInfo _info = new LTJ.Entity.UserGroupInfo();
            if (reader.Read())
            {
                _info.id = TypeConverter.StrToInt(reader["id"].ToString());
                _info.name = reader["name"].ToString();
                _info.sort = TypeConverter.ObjectToInt(reader["sort"]);
                _info.auths = TypeConverter.ObjectToInt(reader["auths"]);
                _info.status = TypeConverter.ObjectToInt(reader["status"]);
                _info.backup = reader["backup"].ToString();
            }
            reader.Close();
            return _info;
        }
        #endregion
    }
}
